﻿using System;

namespace 修为尽失.辅助.exts
{
    public static class Date
    {
        public static string ToDateStringFromNow(this DateTime dt)
        {
            if (!dt.IsValid())
                return string.Empty;
            var timeSpan = DateTime.Now - dt;
            if (timeSpan.TotalDays > 365.0)
                return $"{(object) (int) Math.Floor(timeSpan.TotalDays / 365.0)}年前";
            if (timeSpan.TotalDays > 30.0)
                return $"{(object) (int) Math.Floor(timeSpan.TotalDays / 30.0)}个月前";
            if (timeSpan.TotalDays > 7.0)
                return $"{(object) (int) Math.Floor(timeSpan.TotalDays / 7.0)}周前";
            if (timeSpan.TotalDays > 1.0)
                return $"{(object) (int) Math.Floor(timeSpan.TotalDays)}天前";
            if (timeSpan.TotalHours > 1.0)
                return $"{(object) (int) Math.Floor(timeSpan.TotalHours)}小时前";
            if (timeSpan.TotalMinutes > 1.0)
                return $"{(object) (int) Math.Floor(timeSpan.TotalMinutes)}分钟前";
            if (timeSpan.TotalSeconds >= 1.0)
                return $"{(object) (int) Math.Floor(timeSpan.TotalSeconds)}秒前";
            if (dt.Date == DateTime.Now.Date)
                return "今天";
            timeSpan = dt - DateTime.Now;
            var num = (int) Math.Floor(Math.Abs(timeSpan.TotalDays));
            switch (num)
            {
                case 1:
                    return "明天";
                case 2:
                    return "后天";
                default:
                    if (num < 7)
                        return $"{(object) num}天后";
                    if (num < 30)
                        return $"{(object) (num / 7)}周后";
                    if (num < 365)
                        return $"{(object) (num / 30)}月后";
                    return $"{(object) (num / 365)}年后";
            }
        }

        public static string ToDateStringFromNow(this DateTime? dt)
        {
            if (dt.HasValue)
                return dt.Value.ToDateStringFromNow();
            return string.Empty;
        }
    }
}